package pt.mauromad.guias.dao;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "company")
public class CompanyDao {
	private Long id;
	private AccountDao account;
	private String name;
	private String fiscalNumber;
	private boolean enabled;
	private AddressDao mainAddress;
	private List<AddressDao> addresses;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFiscalNumber() {
		return fiscalNumber;
	}

	public void setFiscalNumber(String fiscalNumber) {
		this.fiscalNumber = fiscalNumber;
	}

	@OneToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY, mappedBy = "company")
	@Fetch(FetchMode.SUBSELECT)
	public List<AddressDao> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<AddressDao> addresses) {
		this.addresses = addresses;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@ManyToOne(cascade = CascadeType.DETACH, optional = false, fetch = FetchType.EAGER)
	@JoinColumn(name = "account_id", insertable = true, updatable = false, nullable = false)
	public AccountDao getAccount() {
		return account;
	}

	public void setAccount(AccountDao account) {
		this.account = account;
	}

	@OneToOne(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
	@JoinColumn(name = "main_address_id")
	public AddressDao getMainAddress() {
		return mainAddress;
	}

	public void setMainAddress(AddressDao mainAddress) {
		this.mainAddress = mainAddress;
	}

	@Transient
	public boolean isMainCompany() {
		return account.getCompany().id.equals(this.id);
	}
}
